package com.mobilepay.moneypig.model.activity.home;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.mobilepay.moneypig.R;
import com.mobilepay.moneypig.base.BaseActivity;
import com.mobilepay.moneypig.base.NoDoubleClickListener;
import com.mobilepay.moneypig.config.Key;
import com.mobilepay.moneypig.config.NetConfig;
import com.mobilepay.moneypig.model.application.App;
import com.mobilepay.moneypig.okhttp.HttpUtils;
import com.mobilepay.moneypig.response.ResponseResult;
import com.mobilepay.moneypig.response.home.ACreditSuccessData;
import com.mobilepay.moneypig.utils.CommonUtil;
import com.mobilepay.moneypig.utils.GsonUtil;
import com.mobilepay.moneypig.utils.PreferenceUtil;
import com.mobilepay.moneypig.view.WebViewActivity;

/**
 * Created by xlm on 2017/7/29 0029
 */

public class ApplyCreditActivity extends BaseActivity {
    private static final String TAG = "ApplyCreditActivity";
    private TextView mTextMe;
    private TextView mTextOther;
    private EditText mEditMobile;
    private EditText mEditName;

    private TextView mTextApllyBtn;

    private RelativeLayout mRelativeBack;

    private static boolean isMe = true;

    private int id;
    private String background;
    private ImageView mImageBg;

    @Override
    protected void onSetContentView() {
        setContentView(R.layout.activity_apply_credit);
    }

    @Override
    protected void onGetIntent(Intent intent) {
        id = intent.getIntExtra("buss_id", 0);
        background = intent.getStringExtra("background");
    }

    @Override
    protected void initViews() {
        mTextMe = (TextView) findViewById(R.id.apply_me);
        mTextOther = (TextView) findViewById(R.id.apply_other);
        mTextApllyBtn = (TextView) findViewById(R.id.apply_small_btn);
        mRelativeBack = (RelativeLayout) findViewById(R.id.apply_back);
        mEditMobile = (EditText) findViewById(R.id.apply_mobile);
        mEditName = (EditText) findViewById(R.id.apply_name);
        mImageBg = (ImageView) findViewById(R.id.credit_img);
    }

    @Override
    protected void afterOnCreate(Bundle savedInstanceState) {
        setMe();
        setCreditBg();
    }

    private void setCreditBg() {
        if (!CommonUtil.isEmpty(background)) {
            Glide.with(getThis())
                    .load(background)
                    .placeholder(R.mipmap.default_cover)
                    .diskCacheStrategy(DiskCacheStrategy.SOURCE)
                    .into(mImageBg);
        }
    }

    @Override
    protected void onSetListener() {
        mTextMe.setOnClickListener(this);
        mTextOther.setOnClickListener(this);
        mTextApllyBtn.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View view) {
                super.onNoDoubleClick(view);
                commitData();
            }
        });
        mRelativeBack.setOnClickListener(this);
    }

    private void setMe() {
        mTextMe.setTextColor(Color.WHITE);
        mTextMe.setBackgroundDrawable(getResources().getDrawable(R.drawable.shape_left_apply_btn));
        mTextOther.setTextColor(Color.BLACK);
        mTextOther.setBackgroundDrawable(getResources().getDrawable(R.drawable.shape_right_apply_btn));
        mEditMobile.setEnabled(false);
        mEditName.setEnabled(false);
        Object mobile = PreferenceUtil.getParam(getThis(), Key.USERNAME, "");
        if (mobile != null) {
            mEditMobile.setText(mobile.toString());
        }
        String name = App.getApp().getUserDetailInfo().getData().getReal_name() + "";
        if (CommonUtil.isEmpty(name)) {
            mEditName.setText("");
        } else {
            mEditName.setText(name);
        }
    }

    private void setOther() {
        mTextMe.setTextColor(Color.BLACK);
        mTextMe.setBackgroundDrawable(getResources().getDrawable(R.drawable.shape_left_btn_white));
        mTextOther.setTextColor(Color.WHITE);
        mTextOther.setBackgroundDrawable(getResources().getDrawable(R.drawable.shape_right_btn_orange));
        mEditMobile.setEnabled(true);
        mEditName.setEnabled(true);
        mEditMobile.setText(null);
        mEditName.setText(null);
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.apply_back:
                finish();
                break;
            case R.id.apply_me:
                setMe();
                isMe = true;
                break;
            case R.id.apply_other:
                setOther();
                isMe = false;
                break;
        }
    }


    String realName;
    String mobile;

    private void commitData() {
        realName = mEditName.getText().toString();
        mobile = mEditMobile.getText().toString();
        String username = App.getApp().getUserInfo().getData().getUsername();
        if (CommonUtil.isEmpty(mobile)) {
            showToastShort("手机号码不能为空");
            return;
        }
        if (CommonUtil.isEmpty(realName) && mobile.equals(username)) {
            showToastShort("请先进行实名认证");
            return;
        }
        if (CommonUtil.isEmpty(realName)) {
            showToastShort("姓名不能为空");
            return;
        }

        HttpUtils.RequestParam param = new HttpUtils.RequestParam();
        param.add("method", NetConfig.commit_business);
        param.add("uid", String.valueOf(App.getApp().getUserInfo().getData().getUid()));
        param.add("real_name", realName);
        param.add("mobile", mobile);
        param.add("product_id", String.valueOf(id));
        param.add("sign", param.getMd5String());
        HttpUtils.getInstance().params(param).executePost(new HttpUtils.HttpListener() {
            @Override
            public void onException(Throwable e) {
                super.onException(e);
                if (CommonUtil.isForeground(getThis())) {
                    showToastShort(getString(R.string.net_state));
                }
            }

            @Override
            public void onHttpSuccess(String string) {
                super.onHttpSuccess(string);
                if (HttpUtils.isJson(string)) {
                    ResponseResult responseResult = GsonUtil.GsonToBean(string, ResponseResult.class);
                    showToastShort(responseResult.getInfo());
                    if (responseResult.getStatus() == 1) {
                        ACreditSuccessData data = GsonUtil.GsonToBean(string, ACreditSuccessData.class);
                        String url = data.getData().getProduct().getUrl();
                        if (!CommonUtil.isEmpty(url)) {
                            Intent intent = new Intent();
                            intent.setClass(getThis(), WebViewActivity.class);
                            intent.putExtra(Key.TITLE, data.getData().getProduct().getTitle());
                            intent.putExtra(Key.URL, url);
                            startActivity(intent);
                        }
                    }
                }
            }
        });
    }
}
